// 页面form 与 接口data 数据结构互相转换
// 食谱留样
import dictUtil2 from '@/common/js/dictUtil2.js';
import {separator, imageDomain} from '@/common/js/resource.js';
import tool from '@/common/js/tools.js';
export default {
	/**
	 * 将接口返回的res.data转换成页面的formData
	 * @param {Object} data 接口返回的data
	 * @return {Object} 页面formData
	 */
	newForm(data) {
		/*
		BaoLiuTiaoJian: "常温"
		BaoLiuTiaoJianID: 19
		CaiPinMingCheng: "糖醋排骨"
		CanCi: "午餐"
		ID: 2
		JiLiangDanWei: "公斤"
		LiuYangCanCiID: 23
		LiuYangJieShuDate: "2024-04-10 11:28:00"
		LiuYangKaiShiDate: "2024-04-10 10:27:00"
		LiuYangRen: "李志洋"
		LiuYangZhaoPianURL: "http://yiyd.sapxw.com/Uploads/TaiZhang/202404/202404101027582758.jpeg"
		LiuYangZhongLiang: 0.1
		QiYeID: 1
		ZhuangTai: 1
		*/
		return {
			id: data.LiuYangJiLuID,
			companyId: data.QiYeID,
			name: data.CaiPinMingCheng, // 菜品名称
			begDate: data.LiuYangKaiShiDate, // 留样开始时间
			expDate: data.LiuYangJieShuDate, // 留样结束时间
			weight: data.LiuYangZhongLiang, // 留样重量
			unitId: data.JiLiangDanWeiID, // 计量单位 选
			unit: dictUtil2.getDictLabel1('unit', data.JiLiangDanWeiID),
			storageId: data.BaoLiuTiaoJianID, // 保留条件 常温/冷藏
			storage: dictUtil2.getDictLabel1('storage', data.BaoLiuTiaoJianID),
			contract: data.LiuYangRen, // 留样人
			mealsId: data.LiuYangCanCiID, // 餐次 早餐、午餐、晚餐
			meals: dictUtil2.getDictLabel1('meals', data.LiuYangCanCiID),
			images1: tool.addImageDomain(data.LiuYangZhaoPianURL), // 留样照片
			remark: data.BeiZhu
		}
	},
	/**
	 * 将接口返回的res.data转换成页面的dataList
	 * @param {Array} list 接口返回的list
	 * @return {Object} 页面dataList
	 */
	newFormList(list) {
		/*
		BaoLiuTiaoJian: "冷藏"
		BaoLiuTiaoJianID: 26
		CaiPinMingCheng: "西红柿炒鸡蛋"
		CanCi: "午餐"
		ID: 1
		JiLiangDanWeiMingCheng: "公斤"
		LiuYangCanCiID: 23
		LiuYangJieShuDate: "2024-03-04 00:00:00"
		LiuYangKaiShiDate: "2024-03-03 00:00:00"
		LiuYangRen: "张无忌"
		LiuYangZhaoPianURL: null
		LiuYangZhongLiang: 2.5
		QiYeID: 1
		RecordCount: 1
		ZhuangTai: 1
		*/
		let arr = list.map(data => {
			return {
				id: data.ID,
				name: data.CaiPinMingCheng, // 菜品名称
				begDate: data.LiuYangKaiShiDate, // 留样开始时间
				expDate: data.LiuYangJieShuDate, // 留样结束时间
				weight: data.LiuYangZhongLiang, // 留样重量
				unit: data.JiLiangDanWeiMingCheng, // 计量单位
			}
		})
		return arr;
	},
	/**
	 * 将页面页面的formData转换成接口需要的data
	 * @param {Object} form 页面formData
	 * @return {Object} 传给接口的data
	 */
	newApiData(data) {
		return {
			LiuYangJiLuID: data.id || '',
			QiYeID: data.companyId,
			CaiPinMingCheng: data.name, // 菜品名称
			LiuYangKaiShiDate: data.begDate, // 留样开始时间
			LiuYangJieShuDate: data.expDate, // 留样结束时间
			LiuYangZhongLiang: data.weight, // 留样重量
			JiLiangDanWeiID: data.unitId, // 计量单位 选
			BaoLiuTiaoJianID: data.storageId, // 保留条件 常温/冷藏
			LiuYangRen: data.contract, // 留样人
			LiuYangCanCiID: data.mealsId, // 餐次 早餐、午餐、晚餐
			LiuYangZhaoPianURL: data.images1.replaceAll(imageDomain, ''), // 留样照片
			BeiZhu: data.remark
		}
	},
	// 比较新form和原form是否相同（无变化）
	compare(form1, form2){
		console.log(form1, form2)
		return form1.name      == form2.name
			&& form1.begDate   == form2.begDate
			&& form1.expDate   == form2.expDate
			&& form1.weight    == form2.weight
			&& form1.unitId    == form2.unitId
			&& form1.storageId == form2.storageId
			&& form1.contract  == form2.contract
			&& form1.mealsId   == form2.mealsId
			&& form1.images1   == form2.images1
			&& form1.remark    == form2.remark
	},
}